import json
from ..Util.NetUtil import sendACId, sendACMsg
from Autodroid.Bean.MyObject import NodeObject


def test():
    return sendACId(10)


def tap(x, y, duration=39):
    data = {
        'x1': x,
        'y1': y,
        'x2': 0,
        'y2': 0,
        'duration': duration
    }
    return sendACMsg(11, data)


def swipe(x1, y1, x2, y2, duration=500):
    data = {
        'x1': x1,
        'y1': y1,
        'x2': x2,
        'y2': y2,
        'duration': duration
    }
    return sendACMsg(11, data)


def swipe_toUp():
    swipe(500, 500, 500, 800, 39)


def swipe_toDown():
    swipe(500, 800, 500, 500, 39)


def swipe_toRight():
    swipe(800, 1000, 500, 1000, 39)


def swipe_toLeft():
    swipe(500, 1000, 800, 1000, 39)


def gesture(): pass


def click(_text, order=0):
    data = {
        'text': _text,
        'order': order,
    }
    result = sendACMsg(12, data, need_result=1)
    print(result)
    return result


def input_text(_text, order=0):
    data = {
        'text': _text,
        'order': order,
    }
    return sendACMsg(13, data)


def text(_text, order=0):
    data = {
        'text': _text,
        'order': order,
    }
    result = sendACMsg(14, data, 1)
    if result is None:
        return NodeObject()
    result = json.loads(result)

    d = NodeObject()
    d.fullId = result['fullId']
    d.className = result['className']
    d.packageName = result['packageName']
    d.text = result['text']
    d.desc = result['desc']

    return d


def fineOne():
    pass
